﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GroupChat2013.Common
{
    /// <summary>
    /// Json转换帮助类
    /// </summary>
    public class JsonConvert
    {
        /// <summary>
        /// 将json字符串转换为对象
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="jsonString">json串</param>
        /// <returns>反序列化后的对象</returns>
        public static T Parse<T>(string jsonString) where T : class,new()
        {
            try
            {
                if (string.IsNullOrEmpty(jsonString))
                    return new T();

                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonString);
            }
            catch { return null; }
        }

        /// <summary>
        /// 将json字符串转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        //public static T LitJsonParse<T>(string jsonString) where T : new()
        //{
        //    if (string.IsNullOrEmpty(jsonString))
        //        return new T();

        //    return JsonMapper.ToObject<T>(jsonString);
        //}

        /// <summary>
        /// 将对象转换为json字符串
        /// </summary>
        /// <param name="jsonObject">需要序列化的对象</param>
        /// <returns>序列化好的json串</returns>
        public static string ConvertToJson(object jsonObject)
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject);
            }
            catch { return string.Empty; }
        }
    }
}
